package com.example.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.example.mapper.CartypeMapper;
import com.example.pojo.Car;
import com.example.pojo.Cartype;
import com.example.service.CarService;
import com.example.mapper.CarMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
* @author Lenovo
* @description 针对表【car】的数据库操作Service实现
* @createDate 2024-04-06 13:08:28
*/
@Service
public class CarServiceImpl extends ServiceImpl<CarMapper, Car>
    implements CarService{
    @Autowired
    private CarMapper carMapper;
    @Autowired
    private CartypeMapper cartypeMapper;

    @Override
    public Integer add(Car edocEntry) {
        return carMapper.insert(edocEntry);
    }

    @Override
    public Page<Car> list(Integer categoryId, String name, Integer startIndex, Integer pageSize) {
        Page<Car> page = new Page<>();
        page.setSize(pageSize);
        page.setCurrent(startIndex);
        QueryWrapper<Car> carQueryWrapper = new QueryWrapper<>();
        if(categoryId != null) {
            carQueryWrapper.eq("carType",categoryId);
        }
        if(name != null) {
            carQueryWrapper.like("carName",name);
        }
        return carMapper.selectPage(page,carQueryWrapper);
    }

    @Override
    public Long count(Integer categoryId) {
        QueryWrapper<Car> carQueryWrapper = new QueryWrapper<>();
        if(categoryId != null) {
            carQueryWrapper.eq("carType",categoryId);
        }
        return carMapper.selectCount(carQueryWrapper);
    }

    @Override
    public List<Cartype> carType() {
        return cartypeMapper.selectList(null);
    }

    @Override
    public int toUpdate(Car edocEntry) {
        return carMapper.updateById(edocEntry);
    }

    @Override
    public int delete(Integer id) {
        return carMapper.deleteById(id);
    }
}




